- /* sdostrm.cpp by K.Tsuru */
- // function ID 356 DRADIX since version 2.21
- /*****************************************************************
- SDouble class
- It provides the output stream.
- usage : cout << x;
- output format : see SetFormat()
-
- "stringstream" is partialy used.
- Mainly "sprintf()" function.
- ******************************************************************/
- #ifndef SN_H
- #include "sn.h"
- #endif
-
- // static objects of SDouble class
- long SDouble::figUnit = 10L;
- long SDouble::putFig = 0;
- int SDouble::perLine = 0;
- int SDouble::lineFormat = SDouble::CRLF|SDouble::ROUND|SDouble::INT_PUT;
- int SDouble::delmt = ' ';
- long SDouble::ioCount = 0; // the number of input or output characters
- long SDouble::crCount = 0;
- // static void SetFormat(long fU, long pF = 0, int pL = 0, int lF = ROUND|INT_PUT|CRLF, int dm = ' '); in sdbl.h
- void SDouble::SetFormat(long fU, long pF, int pL, int lF, int dm){
- figUnit = fU; putFig = pF; perLine = pL; lineFormat = lF;
- delmt = dm;
- } // 359
- void SDouble::DefaultFormat() { // It restores the default values. 360
- SetFormat(10);
- }
-
- //-------------------------------------------------------------------------------
- static int putcr(int cr, ostream& os){
- if(cr) { os << endl; SDouble::crCount++; }
- return cr ? 1 : 0;
- }
- static int putdelmt(char d, ostream& os){
- if(d) os << d;
- return d ? 1 : 0;
- }
- static int putbuff(const char* buff, ostream& os) {
- int len = strlen(buff);
- os.write(buff, len);
- return len;
- }
-
- // function ID = 358
- /*********
- main body
- **********/
- ostream& operator<<(ostream& os, const SDouble& sd) {
- sd.crCount = 0;
- long fig = sd.figUnit, pF = sd.putFig;
- int pL = sd.perLine, fmt =sd.lineFormat, dm = sd.delmt;
-
- sd.SignCheck(358); // sign == UNDECIDED ?
- // FILE* out = FileStream();
-
- if(!fig){ fig = LONG_MAX; dm = 0; } // continuously output with no delimiter
- int showFig = fmt & sd.SHOW_FIG;
- int crlf = fmt & sd.END_CR;
-
- if( sd.Sign(344) == 0 ){
- sd.ioCount = putbuff("0.0", os); sd.ioCount += putcr(crlf, os); return os;
- // fprintf(out, "0.0"); return 3+putcr(crlf, out);
- }
-
- SDouble temp(sd);
-
- long cCount = 0;
- int nc = 0; //the number of characters in a line
- long decCount = 0; //0.abcd|efgh|.... counter of decimal part
-
- temp.StdReform(344); //standard form
-
- // lineFormat temp display initial value of decCount
- // lineFormat & INT_PUT 0|000a|bcde|fgh..... a.bcdefgh... DFIGURES
- // else 0|abcd|efgh|..... 0.abcdefgh.... 0
- uint last; //last position of output
-
- if(sd.lineFormat & sd.ROUND) temp.Round(); //round off
-
- long exp = DFIGURES*(long)temp.RdxExp();//exponent in a decimal system
- ulong v = (ulong)temp.figure(1);
- #ifndef NDEBUG
- assert(v);
- #endif
- //It reforms into "0.abcd ...".
- int k = 0;
- while(v < DRADIX){ v *= 10; k++; }
- k--;
-
- temp.TempPointFree(); //temporally into floating point mode
- if(k > 0) {
- v = (ulong)ipow10(k); temp = DsMult(temp, v); exp -= k;
- }
-
- if(sd.lineFormat & sd.INT_PUT){
- //In order to display such as "a.bcdef...." it multiplies by ten.
- if(pF > 1) pF--;
- temp = DsMult(temp, 10); exp--;
- decCount = DFIGURES;
- }
- temp.PointModePop();
-
- if(sd.lineFormat & sd.ROUND) last = min(temp.Last(), temp.EffFig()); //do not display hidden figures
- else last = temp.Last();
-
- //the number of decimal figures between decCount/DFIGURES and last
- long decfig = long(last - decCount/DFIGURES)*(long)DFIGURES; //maybe last=1
- if(pF > 0) decfig = min(decfig, pF);
- //display the negative sign
- if( temp.Sign(344) == -1 ){ os.put('-'); cCount++; }
- //display the integral part
- char buff[displayWidth+1];
- string sbuff("0");
- stringstream ss;
-
- //temp.Puts();
- // buff[0] = '0'; buff[1] = '\0'; // buff[] = "0"
- if(decCount){
- ss << temp.figure(1);
- sbuff= ss.str(); //buff[0] += temp.figure(1);
- // cout << sbuff << endl;
- }
- if( (decfig + decfig/fig) > (int)displayWidth ){
- sbuff += ".+"; //strcat(buff, ".+");
- if(sd.lineFormat & sd.CONTINUE) sbuff += "\\"; // strcat(buff, "\\");
- cCount += sbuff.length(); os << sbuff; cCount += putcr(1, os); // fprintf(out, "%s\n",buff); //over one line
- } else {
- sbuff += "."; //strcat(buff, ".");
- cCount += sbuff.length(); os << sbuff;// fprintf(out, "%s.",buff); //show point
- nc = (int)cCount;
- }
- //decimal part
- int rest; //rest of characters at last line
- int figWidth; //the width of figure display at the end of line, except ()
- int dec_init = (int)decCount;
-
- if(!decfig){ //decimal part=0
- os.put('0'); cCount++;
- rest = displayWidth - (int)cCount;
- } else {
- //non zero decimal part
- int putdec = 0, crLine, figCount = 0;
- figWidth = iFigures(decfig);
- crLine = (pL > 0 ) ? pL : int( (displayWidth-1)/(fig+1) );
- if(showFig) while((int)displayWidth -crLine*(fig+1)<(figWidth+3) ) crLine--;
- if( crLine<=0 ) crLine = 1;
-
- temp.CharNthDec(-1L);
-
- while(decfig){
- os.put( temp.CharNthDec(decCount) );
- decfig--; cCount++; decCount++; nc++; putdec++;
- if(putdec == fig){
- figCount++; nc++; putdec = 0;
- if( figCount != crLine ) cCount += putdelmt(dm, os);
- }
- if( figCount == crLine ){ //reaches at line end
- if(showFig) {
- #if 0 // sprintf() version (very simple)
- cCount += sprintf(buff, " (%*ld)", figWidth, decCount - dec_init);
- os << buff;
- #else // "stringstream" version (too complicated for me)
- ss.str(""); // clear buffer
- ss.clear(stringstream::goodbit);
- ss << " (";
- ss.width(figWidth); ss.fill(' '); ss << decCount - dec_init;
- ss << ')';
- os << ss.str(); cCount += strlen(ss.str().c_str());
- #endif
- }
- if(dm != ' ') cCount += putdelmt(dm, os);
- if(sd.lineFormat & sd.CONTINUE){ os.put('\\'); cCount++; }
- if(sd.lineFormat & (sd.CRLF|sd.SHOW_FIG)){
- putcr(1, os); cCount++;
- }
- figCount = 0; nc = 0;
- }
- }
- rest = int((fig+1)*crLine -nc);
- }
- //display exponent part
- //"sprintf()" version only below.
- if(exp){
- cCount += sprintf(buff,"e%+ld",exp);
- if(rest < (int)strlen(buff) ){
- if(sd.lineFormat & sd.CONTINUE){ os.put('\\'); cCount++; }
- putcr(1, os); cCount++; rest = displayWidth;
- }
- rest -= putbuff(buff, os); //fprintf(out, "%s", buff);
- }
- //display the number of figures at the last line
- if(showFig && nc && (decCount - dec_init) ){//When nc=0 the display ended.
- char tmpbuff[displayWidth+1];
- sprintf(buff,"(%ld)", decCount - dec_init);
- sprintf(tmpbuff, "%*s%s",rest ," ", buff);
- cCount += putbuff(tmpbuff, os);
- // cCount += fprintf(out, "%*s%s",rest," ", buff);
- }
- cCount += putcr(crlf, os);
- sd.ioCount = cCount;
- return os;
- }
sdostrm.cpp : last modifiled at 2017/08/21 12:07:19(6,755 bytes)
created at 2017/10/07 10:21:14
The creation time of this html file is 2017/10/07 10:30:03 (Sat Oct 07 10:30:03 2017).